﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;

namespace WPFExcelReport
{
    public partial class TAB02SYT
    {
        #region ==WFART===========
        public static readonly DependencyProperty WFARTProperty = DependencyProperty.Register("WFART",
            typeof(string), typeof(TAB02SYT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWFARTChanged));
        public string WFART
        {
            get
            {
                return (string)GetValue(WFARTProperty);
            }
            set
            {
                SetValue(WFARTProperty, value);
            }
        }
        private static void OnWFARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAB02SYT;
            if (src != null)
            {
                src.WFART = (string)e.NewValue;
            }
        }
        #endregion 

        #region ==EXORD===========
        public static readonly DependencyProperty EXORDProperty = DependencyProperty.Register("EXORD",
            typeof(string), typeof(TAB02SYT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXORDChanged));
        public string EXORD
        {
            get
            {
                return (string)GetValue(EXORDProperty);
            }
            set
            {
                SetValue(EXORDProperty, value);

            }
        }
        private static void OnEXORDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TAB02SYT;
            if (src != null)
            {
                src.EXORD = (string)e.NewValue;
                src.OnEXORDChg();
            }
        }
        public virtual void OnEXORDChg()
        { }
        #endregion 
        public TAB02SYT()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABPOS();
            DataContext = MV;
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {

            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
            DTSTART = Helpers.Helper.GetDateAddDays(MV.date, -365);
            DTEND = Helpers.Helper.GetDateAddDays(MV.date, 120);
            NEUDATS = Helpers.Helper.GetDateAddDays(MV.date, -365);
            NEUDATE = MV.date;
            IsShowMaster = true;
            IsShowSY = false;
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            if (DTSTART == "")
                DTSTART = "20150101";
            if (DTEND == "")
                DTEND = "20990101";
            dwData["ABPMANDANT"] = app.Mandant;
            dwData["ABPAUFKRS"] = " AND ABPAUFKRS LIKE '" + KRS + "%'";
            dwData["ABPAUFLFD"] = " AND ABPAUFLFD LIKE '" + LFD + "%'";
            dwData["ABPAUFPOS"] = " AND ABPAUFPOS LIKE '" + POS + "%'";
            dwData["ABPFILLER1"] = " AND ABPLIEFDATN>=" + DTSTART + "  AND ABPLIEFDATN<=" + DTEND;
            dwData["ABPERLKZ"] = " AND ABPERLKZ= ' '";
            if (ERLKZ)
            {
                dwData["ABPERLKZ"] = "";
            }
            SResult = TMV.QABP38(dwData);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);

        }

        private void ImportABPCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            SResult = TMV.QMKRF(dwData);

            foreach (DataRow row in SResult.Tables[0].Copy().Rows)
            {
                WFART = "";
                EXORD = ""; 
                WFART = "F" + Helper.Left(row["ABPART"].ToString().PadRight(20), 20)
                     + Helper.Left(row["ABPVAR"].ToString().PadRight(2), 2)
                     + Helper.Left(row["ABPFARBE"].ToString().PadRight(10), 10)
                     + Helper.Left(row["ABPBESCHICH"].ToString().PadRight(10), 10)
                      + Helper.Left(row["ABPDESS"].ToString().PadRight(10), 10)
                     + Helper.Left(row["ABPCOLORIT"].ToString().PadRight(10), 10)
                        + "".PadRight(7)
                     + Helper.Left(row["WABEZ"].ToString().PadRight(20), 20);
                EXORD = Helper.Left(row["PRODORDER"].ToString().PadRight(11), 11)
                   + WFART
                    + "".PadRight(8)
                    + "".PadRight(20)
                    + "".PadRight(10);
            }
            QueryCommand(null, null);
        }
    }
    public partial class VABPOS
    {
        public DataSet QMKRF(DataRow dwData)
        {

            or1 = OracleDB.CreateCommand();
            or1.CommandText = "SELECT DISTINCT ABFAUFKRS,ABFAUFLFD,ABFAUFPOS,ABFAUFKRS||ABFAUFLFD||ABFAUFPOS AS PRODORDER,JP33 AS WABEZ,JP32 AS FINISHING_METHOD,ABPWAKZ,ABPART,ABPVAR,ABPFARBE,ABPDESS,ABPCOLORIT,ABPBESCHICH,ABPGROE,ABKKDAUFTNR FROM ICC_MKRF"
                 + " ORDER BY ABFAUFKRS,ABFAUFLFD,ABFAUFPOS";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            SResult = Connect2Proxy(or1);
            SResult.Tables[0].TableName = TableName;
            return SResult;

        }
        public void UMKRF(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE ICC_MKRF SET ABPBESCHICH=:ABPBESCHICH: WHERE JP32=:FINISHING_METHOD:";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);
        }
    }
}
